#include "../source/Server_version1.hpp"
#include <string>

int main()
{
    // std::string ip = "127.0.0.1";
    u_int16_t port = 9999;

    muduo::Socket ser_socket;
    ser_socket.CreateServer(port);
    int newfd = ser_socket.Accept();
    muduo::Socket newsocket(newfd);
    while (1)
    {
        DBG_LOG("服务器正在运行中");

        std::string echo = "echo# ";
        char buffer[1024];
        bzero(buffer, sizeof(buffer));

        int n = newsocket.Recv(buffer, sizeof(buffer) - 1, 0);
        DBG_LOG("n:%d", n);

        if (n > 0)
        {
            buffer[n] = 0;
            echo.append(buffer, n);
            DBG_LOG("echo:%s", echo.c_str());

            newsocket.Send(echo.c_str(), sizeof(echo), 0);
            newsocket.Close();
        }
        if (n < 0)
        {
            newsocket.Close();
        }
        sleep(1);
    }
    ser_socket.Close();
}